<?php
class UserService{

	private static $_userCoinModule = null;//平台币

	const DEFAULT_HEADURL = '/static/image/icon.jpg';//默认头像地址


	/**
	 * 拿到用户平台币数
	 * @param $uid  [玩家userid]
	 */
	public static function getUserCoin($uid) {
		$uid = intval($uid);
		$rs = self::_getUserCoinModule()->getCoinInfo($uid);
		if (empty($rs)) {
			return array('coin' => 0);
		}
		return $rs;
	}

	private static function _getUserCoinModule() {
		if (!(self::$_userCoinModule instanceof UserCoinModule)) {
			self::$_userCoinModule = new UserCoinModule();
		}
		return self::$_userCoinModule;
	}


    /**
     * 手机号转换
     * @param $data
     * @return bool|string
     */
    public static function convertPhone($data){
        if (empty($data)){
            return '';
        }
        $url = 'http://api.sec.bbfoxgame.com/api/phone.php?biz=bm-jianlian&phone='.$data;

        return HttpUtil::curlGet($url);
    }

    /**
     * 手机号转换-批量 {"35102418598":"13560432198"}
     * @param $data
     * @param $type
     * @return array
     */
    public static function convertPhoneByBatch($data,$type){
        $result = array();
        if (empty($data)){
            return $result;
        }
        if (!in_array($type,array('encode','decode'))){
            return $result;
        }
        $params = array(
            'type'=> $type,
            'biz'=>'bm-jianlian',
            'sign'=>'',
            'phone' => $data,
        );
        $url = 'http://api.sec.bbfoxgame.com/api/phone.php';
        $res = HttpUtil::curlPost($url,array('param'=>json_encode($params)));
        $res = json_decode($res,true);
        if ($res['map']){
            return $res['map'];
        }
        return $result;
    }

    /**
     * 获取真实的手机号
     * @param $phone
     * @return bool|string
     */
    public static function getReallyPhone($phone){
        if (empty($phone)){
            return $phone;
        }
        if (substr($phone,0,1) != 1){
            return UserService::convertPhone($phone);
        }
        return $phone;
    }

    /**
     * 获取真实的手机号-批量
     * @param $phoneList
     * @return array
     */
    public static function getReallyPhoneByBatch($phoneList){
        $decodePhoneToMap = array();
        if (empty($phoneList)){
            return $decodePhoneToMap;
        }
        $encodePhone = array();
        foreach ($phoneList as $v){
            if (empty($v)){
                continue;
            }
            if (substr($v,0,1) != 1){
                $encodePhone[] = $v;
            }else{
                $decodePhoneToMap[$v] = $v;
            }
        }
        $encodePhone = array_chunk($encodePhone,1000);
        foreach ($encodePhone as $data){
            $res = self::convertPhoneByBatch($data,'decode');
            if ($res){
                foreach ($res as $k=>$v){
                    $decodePhoneToMap[$k] = $v;
                }
            }
        }
        return $decodePhoneToMap;
    }

    /**
     * 用户金币余额
     * @param $userids
     * @param $tixi_id
     * @return bool | array
     */
    public static function userMoneyCoin($userids,$tixi_id) {
        if (empty($userids) || !is_array($userids) || empty($tixi_id)){
            return array();
        }
        $payModule = new PfPayModule($tixi_id);
        $list = $payModule->getAllCoinByCondition("userid in(".implode(',',$userids).")",'userid,coin');

        $userCoin = array();
        foreach ($list as $val) {
            $userCoin[$val['userid']] = $val['coin'] / 100;
        }
        return $userCoin;
    }

    /**
     * 福利币余额
     * @param $userids
     * @param $tixi_id
     * @return bool | array
     */
    public static function userWelfarecoin($userids,$tixi_id) {
        if (empty($userids) || !is_array($userids) || empty($tixi_id)){
            return array();
        }
        $payModule = new PfPayModule($tixi_id);
        $list = $payModule->getAllWelfareCoinByCondition("userid in(".implode(',',$userids).")",'userid,coin');

        $userCoin = array();
        foreach ($list as $val) {
            $userCoin[$val['userid']] = $val['coin'] / 100;
        }
        return $userCoin;
    }

    /**
     * 积分
     * @param $userids
     * @param $tixi_id
     * @return bool | array
     */
    public static function userScore($userids,$tixi_id) {
        if (empty($userids) || !is_array($userids) || empty($tixi_id)){
            return array();
        }
        $payModule = new PfUserScoreModule($tixi_id);
        $list = $payModule->getAllByCondition("userid in(".implode(',',$userids).")",'userid,score');

        $user = array();
        foreach ($list as $val) {
            $user[$val['userid']] = $val['score'];
        }
        return $user;
    }

    /**
     * 分成余额
     * @param $userids
     * @param $tixi_id
     * @return bool | array
     */
    public static function userMoney($userids,$tixi_id) {
        if (empty($userids) || !is_array($userids) || empty($tixi_id)){
            return array();
        }
        $model = new PfUserCoinModule($tixi_id);
        $where = array(
            'userid' => array('in',$userids)
        );
        $list = $model->getTableList('user_money',$where,'userid,money');

        $user = array();
        foreach ($list as $val) {
            $user[$val['userid']] = $val['money'] / 100;
        }
        return $user;
    }

    /**
     * 黑金
     * @param $userids
     * @param $tixi_id
     * @return bool | array
     */
    public static function userBlackCoin($userids,$tixi_id) {
        if (empty($userids) || !is_array($userids) || empty($tixi_id)){
            return array();
        }
        $model = new PfUserCoinModule($tixi_id);
        $where = array(
            'userid' => array('in',$userids)
        );
        $list = $model->getTableList('user_blackcoin',$where,'userid,sum(coin) as coin','',0,0,'userid');

        $user = array();
        foreach ($list as $val) {
            $user[$val['userid']] = $val['coin'] / 100;
        }
        return $user;
    }

    /**
     * 省钱卡
     * @param $userids
     * @param $tixi_id
     * @return bool | array
     */
    public static function userSupercard($userids,$tixi_id) {
        if (empty($userids) || !is_array($userids) || empty($tixi_id)){
            return array();
        }
        $payModule = new PfPayModule($tixi_id);

        $list = $payModule->getTableList('user_supercard_expire',array('userid'=>array('in',$userids)),'userid,supercard_id,supercard_expire_time,is_longtime');

        $user = array();
        foreach ($list as $val) {
            $user[$val['userid']][] = $val;
        }
        return $user;
    }

    /**
     * 超级省钱卡过期状态
     */
    public static function supercardExpireStatus($supercard_expire_time,$is_longtime=0){
        $is_long = '';
        if ($is_longtime == 1){
            $is_long = '（长期）';
        }
        if (empty($supercard_expire_time)){
            return '未开通';
        }
        if ($supercard_expire_time > time()){
            return Common::toDate($supercard_expire_time,'Y-m-d').$is_long;
        }
        if ($is_longtime == 1){
            return Common::toDate($supercard_expire_time,'Y-m-d').$is_long;
        }
        return '已过期';
    }
}